//
//  SQUpdateViewController.m
//  pai
//
//  Created by pro on 14-10-29.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQUpdateViewController.h"
#import "SQNoticeViewController.h"
#import "Global.h"
#import "SQMainViewController.h"

@interface SQUpdateViewController ()

@end

@implementation SQUpdateViewController

@synthesize dialogue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self update];
    [self login];
}
-(void)update
{
    NSString* message = [[NSString alloc]initWithFormat: @"更新？"];
    self.dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [self.dialogue show];
}
-(void)login
{
    UserInfo* userinfo = [[UserInfo alloc] init];
    userinfo = [userinfo doLoad];
    if (userinfo) {
        NSLog(@"自动登录");
        //自动登录
        [Global instance].userinfo = userinfo;
        
        user_login_t login;
        login.base.msgtype = 1011;
        login.base.len = sizeof(login);
        login.base.error = 0;
        login.devicetype = 0;
        strcpy(login.userid, [userinfo.username UTF8String]);
        strcpy(login.userpwd, [userinfo.password UTF8String]);
        strcpy(login.appver, [VERSION UTF8String]);
        if ([Global instance].devicetoken) {
            strcpy(login.devicetoken, [[Global instance].devicetoken UTF8String]);
        }
        else{
            strcpy(login.devicetoken, "");
        }
        
        NSMutableData* msgData = [NSMutableData data];
        [msgData appendBytes:&login length:sizeof(login)];
        
        [super sendData:msgData tag:1 withHUD:YES];
    }else{
        NSLog(@"tiaozhuang");
        [self gotoNotice];
    }
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    user_login_ack_t loginack;
    [data getBytes:&loginack length:sizeof(loginack)];
    if(loginack.base.error!=ACS_SUCCESS)
    {
        //出错
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            [self.HUD show:NO];
        } completionBlock:^{
            //清除本地账号密码
            UserInfo* userinfo = [[UserInfo alloc] init];
            [userinfo doDelete];
            [self gotoNotice];
        }];
        return;
    }
    else{
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            [self.HUD show:NO];
        } completionBlock:^{
            [Global instance].logined = YES;
            UserInfo* userinfo = [[UserInfo alloc] init];
            userinfo = [userinfo doLoad];
            [Global instance].userinfo = userinfo;
            [self gotoNotice];
        }];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex:%d",buttonIndex);
    if (buttonIndex==0) {
        //取消
        [self login];
    }else if(buttonIndex==1){
        //确定
        [self doactionsubmit];
    }
    [alertView setHidden:YES];
}

-(void)doactionsubmit
{
    NSInteger myAppID = 587767923;
    NSString *str = [NSString stringWithFormat:
                     @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",
                     myAppID ];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

-(void)gotoNotice
{
    //SQNoticeViewController* vc = [[SQNoticeViewController alloc]init];
    SQMainViewController* vc = [[SQMainViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
